package com.cloud.cloudshop.provider;

import com.cloud.common.constant.RabbitmqOrderConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * 取消订单消息的发出者
 * .
 */
@Slf4j
@Component
public class DeadOrderSender {

    /**
     * 自动注入RabbitTemplate模板类
     */
    private final RabbitTemplate rabbitTemplate;

    public DeadOrderSender(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }


    public void sendMessage(Long orderId){
        //消息唯一ID
        CorrelationData correlationData = new CorrelationData(orderId+"");
        //给延迟队列发送消息
        rabbitTemplate.convertAndSend(RabbitmqOrderConstants.ORDER_DIRECT_EXCHANGE, RabbitmqOrderConstants.ORDER_DIRECT_ROUTING, orderId, message -> {
            //给消息设置延迟毫秒值
            message.getMessageProperties().setExpiration("8000");
            return message;
        },correlationData);
        log.info("send dead message orderId:{}",orderId);

    }
}